package org.self.activitidemo.controller.client;

import com.alibaba.fastjson.JSONObject;
import org.self.activitidemo.model.Tasks;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.self.activitidemo.service.LeaveWorkflowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

import java.util.Map;



@RestController
@RequestMapping("/leave")
public class LeaveController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private Tasks leave = new Tasks();
    @Autowired
    protected TaskService taskService;
    @Autowired
    protected LeaveWorkflowService leaveWorkflowService;

    private RuntimeService runtimeService;


    /**
     *
     * @param paramsStr
     * var var1为流程中的表达式变量名
     * 若一次性将任务分配，则在variables中将所有变量赋值
     * 若需要在办理后分配，则在调用complete()方法时在variables中将变量赋值
     * {
     * "userId":"userB",
     * "variables":{
     * "var":"user",
     * "var1":"userA",
     * }
     * }
     * @return
     */
    @RequestMapping(value = "/startLeave", method = RequestMethod.POST)
    public String startLeave(@RequestBody String paramsStr) {
        JSONObject result = new JSONObject();
        try {
            JSONObject myJsonObject = JSONObject.parseObject(paramsStr);
            String userId = myJsonObject.getString("userId");
            String processName = myJsonObject.getString("processName");
            String variables = myJsonObject.getString("variables");
            Map<String, Object> variablesMap = (Map)JSON.parse(variables);
            ProcessInstance processInstance = leaveWorkflowService.startWorkflow(userId, processName, variablesMap);
            result.put("status", "success");
            return result.toString();
        }catch (ActivitiException e) {
            if (e.getMessage().indexOf("no processes deployed with key") != -1) {
                logger.warn("没有部署流程!", e);
                result.put("error", "没有部署流程");
                return result.toString();
            } else {
                logger.error("启动请假流程失败：", e);
                result.put("error", "系统内部错误");
                return result.toString();
            }
        }
    }

    /**
     *
     * @param paramsStr
     * {
     * "userId":"userB",
     * }
     * @return
     */
    @RequestMapping(value = "/listLeave", method = RequestMethod.POST)
    public String listLeave(@RequestBody String paramsStr) {
        JSONObject myJsonObject = JSONObject.parseObject(paramsStr);
        return leaveWorkflowService.findTodoTasks(myJsonObject.getString("userId"));
    }

    /**
     *
     * @param paramsStr
     * {
     * "userId":"userB",
     * "taskId":"123",
     * "variables":{
     * }
     * @return
     */
    @RequestMapping(value = "/claim", method = RequestMethod.POST)
    public String claim(@RequestBody String paramsStr) {
        JSONObject myJsonObject = JSONObject.parseObject(paramsStr);
        String taskId = myJsonObject.getString("taskId");
        String userId = myJsonObject.getString("userId");
        return leaveWorkflowService.claim(userId,taskId);
    }

    /**
     *
     * @param paramsStr
     * 若要将下一次任务分配给别人，则在variables中将表达式变量赋值
     * {
     * "userId":"userB",
     * "taskId":"123",
     * "variables":{
     * "流程图中表达式的变量名":"参数"
     * }
     * }
     * @return
     */
    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    public String complete(@RequestBody String paramsStr) {

        JSONObject myJsonObject = JSONObject.parseObject(paramsStr);
        String taskId = myJsonObject.getString("taskId");
        String userId = myJsonObject.getString("userId");
        String variables = myJsonObject.getString("variables");
        Map<String, Object> variablesMap = (Map)JSON.parse(variables);
        return leaveWorkflowService.complete(userId, taskId, variablesMap);

    }
    @Autowired
    public void setRuntimeService(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }
}

